package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.VersionUpdatesService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.app.VersionUpdates;
import com.iot08.common.response.Response;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/10/23 17:00
 * @author: Rock
 */
@Controller
@RequestMapping("version")
public class VersionUpdatesController extends BaseController {

    @Resource
    private VersionUpdatesService versionUpdatesService;

    /**
     * @param page
     * @time: 2020/10/23 17:11
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("list")
    public ModelAndView list(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<VersionUpdates> pages = versionUpdatesService.list(page, this.getSessionUser());
        mv.addObject("versions", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("version/version_list");
        return mv;
    }

    /**
     * @param
     * @time: 2020/10/23 18:15
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("data", versionUpdatesService.getAdd());
        mv.setViewName("version/version_add");
        return mv;
    }

    /**
     * @time: 2020/10/23 18:15
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ModelAndView add(HttpServletRequest request) throws IOException {
        ModelAndView mv = new ModelAndView();
        versionUpdatesService.add(request, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
//        mv.setViewName("version/version_list");
        return mv;
    }

    /**
     * @param id
     * @time: 2020/10/23 18:18
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("toEdit")
    public ModelAndView toEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("data", versionUpdatesService.getEdit(id));
        mv.setViewName("version/version_edit");
        return mv;
    }

    /**
     * @param channel
     * @time: 2020/10/23 18:36
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public ModelAndView edit(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        versionUpdatesService.update(request, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * @param id
     * @time: 2020/10/23 18:39
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public Response<Object> del(String id) {
        return versionUpdatesService.del(id, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * @param id
     * @time: 2020/10/26 10:17
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping("show")
    public ModelAndView show(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("data", versionUpdatesService.show(id));
        mv.setViewName("version/version_show");
        return mv;
    }

    /**
     * @param id
     * @time: 2020/10/26 18:37
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "updateStatus", method = RequestMethod.POST)
    public Response<Object> updateStatus(String id) {
        return versionUpdatesService.updateStatus(id, this.getSessionUser(), this.getRemortIP());
    }

    /**
     * @param id
     * @time: 2020/10/26 18:37
     * @author: Rock
     * @version 1.0
     */
    @ResponseBody
    @RequestMapping(value = "forceUpdateStatus", method = RequestMethod.POST)
    public Response<Object> forceUpdateStatus(String id, Integer status) {
        return versionUpdatesService.forceUpdateStatus(id, status, this.getSessionUser(), this.getRemortIP());
    }


}
